﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.BLL;

public partial class Manage_System_AppItem : System.Web.UI.Page
{
    //protected List<KeRui.Model.CmsModel> listCmsModel;
    //protected KeRui.BLL.CmsModel bllCmsModel;
    protected string ActName = "添加";
    protected string ActFlag = "AddSave";
    protected KeRui.BLL.SysApp bllSysApp = new KeRui.BLL.SysApp();
    protected KeRui.Model.SysApp modelSysApp = new KeRui.Model.SysApp();
    protected List<KeRui.Model.CmsField> listCmsField;
    private ManageErrMsg nsb = new ManageErrMsg();
    //protected string show;

    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//判断登录
        //bllCmsModel = new CmsModel();
        //listCmsModel = bllCmsModel.GetList();
        KeRui.BLL.CmsField bllCmsField = new CmsField();
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                AddSave();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            default: break;
        }
   
    }

    #region 添加
    private void AddSave()
    {
        ManageHelper.CheckAdminPower("system_appadd");//权限
        modelSysApp.AppName = PageRequest.GetString("AppName");
        if (modelSysApp.AppName == "")
        {
            nsb.AddErr("请输入应用程序名称。");
        }
        modelSysApp.AppUrl = PageRequest.GetString("AppUrl");
        if (modelSysApp.AppUrl == "")
        {
            nsb.AddErr("请输入应用程序网址");
        }
        modelSysApp.AppKey = PageRequest.GetString("AppKey");
        if (modelSysApp.AppKey == "")
        {
            nsb.AddErr("请输入应用程序通讯密钥。");
        }
        modelSysApp.IsUsing = PageRequest.GetInt("IsUsing", 0) == 0 ? false : true;
        modelSysApp.IsSystem = 1;   //默认设置为本系统应用程序
        modelSysApp.CreateTime = DateTime.Now;
        //modelSysApp.ModelID = PageRequest.GetInt("ModelID", -1) ;
        //if (modelSysApp.ModelID == -1)
        //{
        //    nsb.AddErr("请选择内容模型。");
        //}
        nsb.ChkErr();
        if (bllSysApp.Add(modelSysApp) < 0)
        {
            nsb.AddErr("对不起！已存在相同的应用程序。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_appadd", "添加了“" + modelSysApp.AppName + "”应用程序。");//写日志
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"appList.aspx?sucmsg=添加成功。\";</script>");
        }

    }
    #endregion

    #region 反显编辑页面
    private void Edit()
    {
        ActName = "修改";
        ActFlag = "editsave";
        int id = PageRequest.GetInt("id", -1);
        if (id == -1)
        {
            nsb.AddErr("error1。"); nsb.ChkErr();
        }
        modelSysApp = bllSysApp.GetModel(id);
        if (modelSysApp == null)
        {
            nsb.AddErr("对不起您想要修改的信息不存在。"); nsb.ChkErr();
        }
    }
    #endregion

    #region 修改
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_appedit");//权限
        modelSysApp.AppID = PageRequest.GetInt("AppID", -1);
        modelSysApp.AppName = PageRequest.GetString("AppName");
        if (modelSysApp.AppName == "")
        {
            nsb.AddErr("请输入应用程序名称。");
        }
        modelSysApp.AppUrl = PageRequest.GetString("AppUrl");
        if (modelSysApp.AppUrl == "")
        {
            nsb.AddErr("请输入应用程序网址");
        }
        modelSysApp.AppKey = PageRequest.GetString("AppKey");
        if (modelSysApp.AppKey == "")
        {
            nsb.AddErr("请输入应用程序通讯密钥。");
        }
        modelSysApp.IsUsing = PageRequest.GetInt("IsUsing", 0) == 0 ? false : true;
        modelSysApp.CreateTime = DateTime.Now;
        //modelSysApp.ModelID = PageRequest.GetInt("ModelID", -1);
        //if (modelSysApp.ModelID == -1)
        //{
        //    nsb.AddErr("请选择内容模型。");
        //}
        nsb.ChkErr();
        if (bllSysApp.Update(modelSysApp) < 0)
        {
            nsb.AddErr("对不起！已存在相同的应用程序标记。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_appedit", "修改了“" + modelSysApp.AppName + "”应用程序。");//写日志
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"appList.aspx?sucmsg=修改成功。\";</script>");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_appdel");//权限
        int id = PageRequest.GetInt("ID", -1);
        int ft = PageRequest.GetInt("ft", -1);
        if (id == -1)
        {
            nsb.AddErr("对不起您所要删除的数据不存在。");
            nsb.ChkErr();
        }
        modelSysApp = bllSysApp.GetModel(id);
        if (modelSysApp == null)
        {
            nsb.AddErr("对不起你要删除的应用程序信息不存在。"); nsb.ChkErr();
        }
        if (bllSysApp.Delete(id) > 0)
        {
            ManageHelper.AddLog("system_appdel", "删除了“" + modelSysApp.AppName + "”应用程序。");//写日志
            Response.Write("<script type=\"text/javascript\">window.location.href=\"appList.aspx?ft=" + ft + "&sucmsg=恭喜！删除成功。\";</script>");
        }
        else
        {
            Response.Write("<script type=\"text/javascript\">window.location.href=\"appList.aspx?ft=" + ft + "&errmsg=对不起删除失败！原因：该应用程序为CMS应用程序。\";</script>");
        }
    }
    #endregion
}
